﻿using HomagChina.Basic.CCT.Db.CCT_Db;
using HomagGroup.Base.UI;
using HomagGroup.Base.UI.Controls;
using Proxy;
using System;
using System.Collections.Generic;
using localText = Proxy.Properties.Resources;

namespace HomagChina.CCT.ViewModels
{
    public class SettingsAppliancesDialogViewModel : NotifyPropertyChangedBase
    {
        #region Handle
        private readonly InsertInfoHandle Handle_Insert;
        private readonly SearchInfoHandle Handle_Search;
        private readonly ModifyInfoHandle Handle_Modify;
        private readonly CommonHandle commonHandle;
        public RelayCommand ApplyCommand { get; }
        #endregion

        #region 属性
        private int _ID;
        private string _MachineName;
        private string _IP;
        private int _Port;
        private string _Topic;
        private string _Describe;
        private Boolean _State;
        private Boolean _AgainstState;

        public string MachineName
        {
            get => _MachineName;
            set
            {
                _MachineName = value;
                OnPropertyChanged();
            }
        }
        //是否为编辑状态
        private bool _IsEdit;

        public int Port
        {
            get => _Port;
            set { _Port = value; OnPropertyChanged(); }
        }

        public int ID
        {
            get => _ID;
            set { _ID = value; OnPropertyChanged(); }
        }

        public Boolean State
        {
            get => _State;
            set { _State = value; OnPropertyChanged(); }
        }

        public string IP
        {
            get => _IP;
            set { _IP = value; OnPropertyChanged(); }
        }

        public bool AgainstState
        {
            get => _AgainstState;
            set { _AgainstState = value; OnPropertyChanged(); }
        }

        public bool IsEdit
        {
            get => _IsEdit;
            set { _IsEdit = value; OnPropertyChanged(); }
        }

        public DropDownList AvailableChannels { get; set; }

        /// <summary>
        /// Dropdown list
        /// </summary>
        private object _SelectedValue;

        public object SelectedValue
        {
            get => _SelectedValue;
            set
            {
                _SelectedValue = value;
                OnPropertyChanged();
            }
        }

        #endregion

        //构造函数
        public SettingsAppliancesDialogViewModel(SetttingAppliancesModel SelectItem, Boolean IsEdit)
        {
            this.IsEdit = IsEdit;
            ApplyCommand = new RelayCommand(Apply);
            commonHandle = new CommonHandle();
            Handle_Search = new SearchInfoHandle();
            Handle_Insert = new InsertInfoHandle();
            Handle_Modify = new ModifyInfoHandle();
            LoadDropDownList();
            if (SelectItem != null)
            {
                this.ID = SelectItem.Id;
                this.IP = SelectItem.IP;
                this.MachineName = SelectItem.MachineNo;
                this.AvailableChannels.SelectedValue = SelectItem.AvailableChannels;
            }
        }

        /// <summary>
        /// 提交函数
        /// </summary>
        private void Apply()
        {

            if (IsEdit)
            {
                var t = new T_MachineInfo
                {
                    IP = this._IP,
                    MachineNo = this.MachineName,
                    AvailableChannels = AvailableChannels.SelectedValue.ToString()
                };
                if (Handle_Modify.ModifyMachineConfig(t, this.ID))
                {
                    HomagGroup.Base.UI.Windows.MessageBox.Show(localText.Added_successfully);
                    commonHandle.CloseDiaLog(localText.SettingsAppliancesDialog);
                }
                else
                {
                    HomagGroup.Base.UI.Windows.MessageBox.Show(localText.Add_Failure);
                }
            }
            else
            {
                var t = new T_MachineInfo
                {
                    IP = this._IP,
                    MachineNo = this.MachineName,
                    AvailableChannels = AvailableChannels.SelectedValue.ToString(),
                    CreationDate = DateTime.Now
                };
                if (Handle_Insert.InsertSettingsHandle(t))
                {
                    HomagGroup.Base.UI.Windows.MessageBox.Show(localText.Added_successfully);
                    commonHandle.CloseDiaLog(localText.SettingsAppliancesDialog);
                }
                else
                {
                    HomagGroup.Base.UI.Windows.MessageBox.Show(localText.Add_Failure);
                }
            }
        }

        /// <summary>
        /// 加载DropDownList内容
        /// Load the DropDownList
        /// </summary>
        public void LoadDropDownList()
        {
            AvailableChannels = new DropDownList();
            var Modes = new List<string>() { { "Both" }, { "Left" }, { "Right" } };
            AvailableChannels.ItemsSource = (Modes);
            AvailableChannels.SelectedIndex = 0;
            AvailableChannels.SelectionChanged += DropDownList_SelectionChanged;
        }

        /// <summary>
        /// DropDownList 选择变化事件
        /// SelectionChanged of DropDownList
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DropDownList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            SelectedValue = ((DropDownList)sender).SelectedItem.ToString();
        }
    }
}